/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xuidemo.view;

import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import com.xuexiang.xuidemo.ResourceTable;

/**
 * 设置文本框自动上浮和下浮隐藏
 *
 * @since 2021-04-15
 */
public class Animationtextview extends DirectionalLayout {
    private EventHandler handler = new EventHandler(EventRunner.create(true));
    private boolean isInfo = true;
    private float movestart;
    private final Context context;
    private Text text;
    private int height;
    private static  final int anInt2000 = 2000;

    /**
     * 构造方法
     *
     * @param context 上下文
     */
    public Animationtextview(Context context) {
        super(context);
        this.context = context;
        inteview();
    }

    /**
     * 构造方法
     *
     * @param context 上下文
     * @param attrSet 样式
     */
    public Animationtextview(Context context, AttrSet attrSet) {
        super(context, attrSet);
        this.context = context;
        inteview();
    }

    /**
     * 构造方法
     *
     * @param context 上下文
     * @param attrSet 样式
     * @param styleName name
     */
    public Animationtextview(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        this.context = context;
        inteview();
    }

    private void inteview() {
        height = AttrHelper.vp2px(60, context);
        Component componenttext = LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_animation_textview,
            this, true);
        text = (Text) componenttext.findComponentById(ResourceTable.Id_animtion_text);
    }

    /**
     * 设置显示和隐藏动画
     *
     * @param component 开始动画的控件
     * @param data text显示文本
     * @param component2 全局显示的布局
     */
    public void seteffect(Component component, String data, Component component2) {
        component.setVisibility(Component.VISIBLE); // 开始是隐藏的
        text.setText("Query:" + data);
        if (isInfo) {
            isInfo = false;
            movestart = component2.getHeight();
        }
        AnimatorProperty property = new AnimatorProperty();
        property.moveFromY(movestart);
        property.moveToY(movestart - height); // 距离位置
        property.setTarget(component); // 基于那个控件
        property.setDuration(anInt2000);
        property.start();
        handler.postTask(new Runnable() {
            @Override
            public void run() {
                getContext().getUITaskDispatcher().asyncDispatch(new Runnable() {
                    @Override
                    public void run() {
                        AnimatorProperty property = new AnimatorProperty();
                        property.moveFromY(movestart - height); // 开始距离y轴的距离
                        property.moveToY(movestart); // 结束距离位置
                        property.setTarget(component);
                        property.setDuration(anInt2000);
                        property.start();
                    }
                });
            }
        }, anInt2000);
    }
}
